﻿using System.Globalization;

namespace LFSRecord2.Misc
{
    public static class TimeHelper
    {
        public static string FrameNumToPlayHeadText(int frameNum, int fps)
        {
            bool negative = false;
            if (frameNum < 0)
            {
                negative = true;
                frameNum = -frameNum;
            }
            int framesRemain = frameNum % fps;
            int seconds = (frameNum - framesRemain) / fps;
            int secondsRemain = seconds % 60;
            int minutesRemain = (seconds - secondsRemain) / 60;

            char padChar = (char)48;
            return ((negative) ? "-" : "") + minutesRemain + ":" + ((string)"" + secondsRemain).PadLeft(2, padChar) + ":" + ((string)"" + framesRemain).PadLeft(2, padChar);
        }

        public static bool PlayHeadTextToFrameNum(string text, int fps, ref double result)
        {
            char splitColon = char.Parse(":");
            char splitDot = char.Parse(".");
            int hours = 0;
            int minutes = 0;
            int seconds = 0;
            int millis = 0;
            int frames = 0;
            bool hasMilli = false;

            if (text.Length == 0)
                return false;

            // split up the items
            string[] exp2;
            string[] exp = text.Split(splitColon);

            //Debug.WriteLine("Num items : {0}", exp.Length);

            if (exp.Length == 1 && !exp[0].Contains("."))
            {
                //Debug.WriteLine("The one item : " + exp[0]);
                // User entered one number only - we assume it's seconds
                if (!int.TryParse(exp[0], NumberStyles.Integer, CultureInfo.InvariantCulture, out seconds))
                    return false;
                result = seconds * fps;
                return true;
            }

            // Run through the items in reverse
            int step = 0;
            for (int a = exp.Length-1; a >= 0 ; a--)
            {
                //Debug.WriteLine("Item {0} : {1}", step, exp[a]);
                if (exp[a].Length == 0)
                    exp[a] = "0";
                
                if (step == 0)
                {
                    // Check if the last item has a dot. If so, the user meant to enter milliseconds and we have to parse both them and seconds here
                    if (exp[a].Contains("."))
                    {
                        hasMilli = true;
                        exp2 = exp[a].Split(splitDot);
                        //Debug.WriteLine("Num Subitems : {0}", exp2.Length);
                        for (int b = 0; b < exp2.Length; b++)
                        {
                            //Debug.WriteLine("Subitem {0} : {1}", b, exp2[b]);
                            if (exp2[b].Length == 0)
                                exp2[b] = "0";

                            if (b == 0)
                            {
                                // Seconds
                                if (!int.TryParse(exp2[b], NumberStyles.Integer, CultureInfo.InvariantCulture, out seconds))
                                    return false;
                                step++;
                            }
                            else if (b == 1)
                            {
                                // Milliseconds
                                exp2[b] += "00";
                                exp2[b] = exp2[b].Substring(0, 3);
                                if (!int.TryParse(exp2[b], NumberStyles.Integer, CultureInfo.InvariantCulture, out millis))
                                    return false;
                            }
                        }
                    }
                    else
                    {
                        // Parse frames
                        if (!int.TryParse(exp[a], NumberStyles.Integer, CultureInfo.InvariantCulture, out frames))
                            return false;
                    }
                }
                else if (step == 1)
                {
                    // Seconds
                    if (!int.TryParse(exp[a], NumberStyles.Integer, CultureInfo.InvariantCulture, out seconds))
                        return false;
                }
                else if (step == 2)
                {
                    // Minutes
                    if (!int.TryParse(exp[a], NumberStyles.Integer, CultureInfo.InvariantCulture, out minutes))
                        return false;
                }
                else if (step == 3)
                {
                    // Hours
                    if (!int.TryParse(exp[a], NumberStyles.Integer, CultureInfo.InvariantCulture, out hours))
                        return false;
                }

                step++;
            }

            if (hasMilli)
            {
                //Debug.WriteLine("Split time (millis) : {0}:{1}.{2}", minutes, seconds, millis);
                result = hours * 3600 + minutes * 60 * fps + seconds * fps + (millis / 1000.0d) * fps;
            }
            else
            {
                //Debug.WriteLine("Split time (frames) : {0}:{1}:{2}", minutes, seconds, frames);
                result = hours * 3600 + minutes * 60 * fps + seconds * fps + frames;
            }

            return true;
        }

        public static string Seconds2TimeStamp(double time)
        {
            bool negative = (time < 0);
            if (negative)
                time = -time;

            int ms = (int)((time - (int)time) * 1000);
            int mins = (int)time / 60;
            int secs = (int)time - mins * 60;
            int hours = mins / 60;
            mins -= hours * 60;

            char padChar = (char)48;
            if (hours > 0)
                return ((negative) ? "-" : "") + hours + ":" + ((string)"" + mins).PadLeft(2, padChar) + ":" + ((string)"" + secs).PadLeft(2, padChar) + "." + ((string)"" + ms).PadLeft(3, padChar);
            else
                return ((negative) ? "-" : "") + mins + ":" + ((string)"" + secs).PadLeft(2, padChar) + "." + ((string)"" + ms).PadLeft(3, padChar);
        }
    }
}
